package com.seventh.parkingrent.common;


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 白秋洋
 * @Date: 2021/05/14/14:38
 * @Description:发送邮件的工具类
 */
public class MailUtil {
    //创建session对象
    public static void sendMail(String origin,String password,String target,String subject,String emailMsg)throws Exception{
        //创建session对象
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol","SMTP");
        properties.setProperty("mail.host","smtp.163.com");
        properties.setProperty("mail.smtp.auth","true");
        //创建验证器
        Authenticator authenticator = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication(origin,password);
            }
        };
        Session session = Session.getInstance(properties, authenticator);
        //Message对象
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(origin));//设定邮件发送者
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(target));
        message.setSubject(subject);

        message.setContent(emailMsg,"text/html;charset=utf-8");

        //发送邮件
        Transport.send(message);
    }
    public static void main(String[] args) throws Exception {
        sendMail("bqy1037239142@163.com","RWSOXUIQLSXFZKCK","1037239142@qq.com","hello world","今天出大太阳了，好热！");
    }


}
